﻿using System.Web.Services;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Cit.CDE.Net.Service;
using Cit.CDE.Net.Module;
using cde.cache;
using Module;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using System;
using Common.Functions;
using System.IO;
using AjaxControlToolkit;
using System.Collections.Specialized;

/// <summary>
/// Copyright (C)    jinggubang
/// FileName:        SearCodintion    
/// Author :         fengsen
/// CDT:             2011-1-13           
/// Version:         1.0.0.0                
/// Depiction:       列表页面联动查询
/// </summary>
[ScriptService()]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SearCodintion : System.Web.Services.WebService
{

    public SearCodintion()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    private const string display = " ";

    [WebMethod]
    public string TurnTo(string obj_id, string obj_value, string emname, int time)
    {
        if (emname.Equals(WEBConstants.EM_LORE_MANAGE) || emname.Equals(WEBConstants.EM_LORETYPE_MANAGE))
        {
            string result = "";
            if (obj_id.EndsWith("TYPE_CODE"))
            {
                result = City(obj_id, obj_value, emname, time);
            }

            return result;
        }
        return null;
    }



    /// <summary>
    /// 第一级
    /// </summary>
    /// <param name="knownCategoryValues"></param>
    /// <param name="category"></param>
    /// <returns></returns>
    [WebMethod]
    public CascadingDropDownNameValue[] GetLoreType1(string knownCategoryValues, string category)
    {
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

        CDEEntityManager em = CDEFactory.getEMByFullName(WEBConstants.EM_LORETYPE_AJAX);
        CDESearchCriteria sc = new CDESearchCriteria(em);
        sc.addCondition("TYPE_CODE", "0000", CDEConstCompare.LikeRight);
        sc.addCondition("DEL_FLG", "0");
        List<CDEEntity> list = CDEService.LoadEntities(em, sc);

        foreach (CDEEntity entity in list)
        {
            values.Add(new CascadingDropDownNameValue(entity.get("TYPE_NAME").ToString(), entity.get("TYPE_CODE").ToString()));
        }


        return values.ToArray();
    }
    /// <summary>
    /// 第二级
    /// </summary>
    /// <param name="knownCategoryValues"></param>
    /// <param name="category"></param>
    /// <returns></returns>
    [WebMethod]
    public CascadingDropDownNameValue[] GetLoreType2(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        int LoreType1id;


        if (!kv.ContainsKey("LoreType1") || !Int32.TryParse(kv["LoreType1"], out LoreType1id))
        {
            return null;
        }

        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

        CDEEntityManager em = CDEFactory.getEMByFullName(WEBConstants.EM_LORETYPE_AJAX);
        CDESearchCriteria sc = new CDESearchCriteria(em);
        sc.addCondition("TYPE_CODE", LoreType1id.ToString().Substring(0, 2), CDEConstCompare.LikeLeft);
        sc.addCondition("TYPE_CODE", "00", CDEConstCompare.LikeRight);
        sc.addCondition("TYPE_CODE", LoreType1id.ToString(), CDEConstCompare.Inequality);
        sc.addCondition("DEL_FLG", "0");
        List<CDEEntity> list = CDEService.LoadEntities(em, sc);

        foreach (CDEEntity entity in list)
        {
            values.Add(new CascadingDropDownNameValue(entity.get("TYPE_NAME").ToString(), entity.get("TYPE_CODE").ToString()));
        }


        return values.ToArray();
    }
    /// <summary>
    /// 第三级
    /// </summary>
    /// <param name="knownCategoryValues"></param>
    /// <param name="category"></param>
    /// <returns></returns>
    [WebMethod]
    public CascadingDropDownNameValue[] GetLoreType3(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        int LoreType2id;


        if (!kv.ContainsKey("LoreType2") || !Int32.TryParse(kv["LoreType2"], out LoreType2id))
        {
            return null;
        }

        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

        CDEEntityManager em = CDEFactory.getEMByFullName(WEBConstants.EM_LORETYPE_AJAX);
        CDESearchCriteria sc = new CDESearchCriteria(em);
        sc.addCondition("TYPE_CODE", LoreType2id.ToString().Substring(0, 4), CDEConstCompare.LikeLeft);
        sc.addCondition("TYPE_CODE", LoreType2id.ToString(), CDEConstCompare.Inequality);
        sc.addCondition("DEL_FLG", "0");
        List<CDEEntity> list = CDEService.LoadEntities(em, sc);

        foreach (CDEEntity entity in list)
        {
            values.Add(new CascadingDropDownNameValue(entity.get("TYPE_NAME").ToString(), entity.get("TYPE_CODE").ToString()));
        }
        return values.ToArray();
    }

    /// <summary>
    /// 区域
    /// </summary>
    /// <param name="obj_id"></param>
    /// <param name="obj_value"></param>
    /// <param name="emname"></param>
    /// <param name="time"></param>
    /// <returns></returns>
    private string City(string obj_id, string obj_value, string emname, int time)
    {
        //序列化
        JavaScriptSerializer json = new JavaScriptSerializer();
        //添加条件和初始值
        List<string> rtn = new List<string>();
        rtn.Add(obj_id);
        rtn.Add(obj_value);

        if (obj_value.Length < 2 * time)
        {
            return json.Serialize(rtn.ToArray());
        }
        //第一个查询条件
        string value = obj_value.Substring(0, 2 * time);
        //原始em
        CDEEntityManager em = CDEFactory.getEMByFullName(emname);
        //结果列表
        List<CDEEntity> list = null;

        foreach (CDECondition cd in em.ListConditions)
        {
            if (cd.DISP_type != CDEConstants.ConstDispType_HIDDEN && obj_id.EndsWith(cd.Name))
            {
                //根据描述和条件获得结果
                string r_em_fn = cd.Description;
                em = CDEFactory.getEMByFullName(r_em_fn);
                CDESearchCriteria sc = new CDESearchCriteria(em);
                sc.addCondition("TYPE_CODE", value, Cit.CDE.Net.Module.Commons.CDEConstCompare.LikeLeft);
                sc.addCondition("CITY_LEVEL", time + 1);
                list = cde.SystemCommon.CDEService.LoadEntities(em, sc);
            }
        }
        //添加结果并返回
        object[] ary = new object[2];
        ary[0] = value;
        ary[1] = display;
        rtn.Add(json.Serialize(ary));
        if (list.Count > 0)
        {
            foreach (CDEEntity entity in list)
            {
                if (entity.get("TYPE_CODE").ToString() != obj_value)
                {
                    ary[0] = entity.get("TYPE_CODE");
                    ary[1] = entity.get("TYPE_NAME");
                    rtn.Add(json.Serialize(ary));
                }
            }
        }
        return json.Serialize(rtn.ToArray());
    }
    [WebMethod]
    public string uploadPhoto(string photo,string id)
    {   //序列化
        JavaScriptSerializer json = new JavaScriptSerializer();
        //添加条件和初始值
        List<string> rtn = new List<string>();

        byte[] newByte = Convert.FromBase64String(photo);

        string picpath = "~/IDphoto/" + id + ".bmp";
        FileStream FSwriter = new FileStream(Server.MapPath(picpath), FileMode.Create, FileAccess.ReadWrite);
        FSwriter.Write(newByte, 0, newByte.Length);
        FSwriter.Flush();
        FSwriter.Close();
        rtn.Add("IDphoto/" + id + ".bmp");

        return json.Serialize(rtn.ToArray());
    }


}

